/*
    Copyright (C) 2010-2011 mdis
    
    This file is part of mdis.
    Written by Laügt Franz <znarf94@yahoo.fr>,
               Ketfi-Chérif Mehdi <ketficherif,mehdi@gmail.com>

    mdis is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    mdis is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with mdis.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "global.h"

uint8_t str2byte(uint8_t * str)
{
		uint8_t val_h, val_l;
		val_h = *str;
		if(val_h >= '0' && val_h <= '9')
			val_h -= '0';
		else if(val_h >= 'A' && val_h <= 'F')
			val_h -= 55;
		else if(val_h >= 'a' && val_h <= 'f')
			val_h -= 87;

		val_l = *(str+1);
		if(val_l >= '0' && val_l <= '9')
			val_l -= '0';
		else if(val_l >= 'A' && val_l <= 'F')
			val_l -= 55;
		else if(val_l >= 'a' && val_l <= 'f')
			val_l -= 87;

		return (val_h<<4)+val_l;
}

uint32_t strnc ( char * str , char c )
{
	uint32_t n = 0;
	while ( *str )
	{
		if ( *str == c )
			n++;
		str++;
	}
	return n;
}

uint32_t strlenln ( char *str )
{
	uint32_t n = 0 , i = 0;
	while ( *str )
	{
		if ( *str == '\n' )
		{
			if ( i > n )
				n = i;
			i = 0;
		}
		str++;
		i++;
	}
	return n;
}

void
 byte_to_bin ( uint8_t byte , char * str )
{
	uint8_t i;
	for ( i = 0; i < 8 ; i++ )
	{
		if ( byte&(1<<i) )
			str[7-i] = '1';
		else
			str[7-i] = '0';
	}
	str[8] = 0;
}
